// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#ifdef POLLUX_ENABLE_GCS
#include <pollux/common/config/Config.h>
#include <pollux/connectors/hive/storage_adapters/gcs/GcsFileSystem.h> // @manual
#include <pollux/connectors/hive/storage_adapters/gcs/GcsUtil.h> // @manual
#include <pollux/dwio/common/FileSink.h>
#endif

namespace kumo::pollux::filesystems {

#ifdef POLLUX_ENABLE_GCS
melon::once_flag GcsInstantiationFlag;

std::function<std::shared_ptr<
    FileSystem>(std::shared_ptr<const config::ConfigBase>, std::string_view)>
gcsFileSystemGenerator() {
  static auto filesystemGenerator =
      [](std::shared_ptr<const config::ConfigBase> properties,
         std::string_view filePath) {
        // Only one instance of GCSFileSystem is supported for now (follow S3
        // for now).
        // TODO: Support multiple GCSFileSystem instances using a cache
        // Initialize on first access and reuse after that.
        static std::shared_ptr<FileSystem> gcsfs;
        melon::call_once(GcsInstantiationFlag, [&properties]() {
          std::shared_ptr<GcsFileSystem> fs;
          if (properties != nullptr) {
            fs = std::make_shared<GcsFileSystem>(properties);
          } else {
            fs = std::make_shared<GcsFileSystem>(
                std::make_shared<config::ConfigBase>(
                    melon::F14FastMap<std::string, std::string>()));
          }
          fs->initializeClient();
          gcsfs = fs;
        });
        return gcsfs;
      };
  return filesystemGenerator;
}

std::unique_ptr<pollux::dwio::common::FileSink> gcsWriteFileSinkGenerator(
    const std::string& fileURI,
    const pollux::dwio::common::FileSink::Options& options) {
  if (isGcsFile(fileURI)) {
    auto fileSystem =
        filesystems::getFileSystem(fileURI, options.connectorProperties);
    return std::make_unique<dwio::common::WriteFileSink>(
        fileSystem->openFileForWrite(fileURI, {{}, options.pool, std::nullopt}),
        fileURI,
        options.metricLogger,
        options.stats);
  }
  return nullptr;
}
#endif

void registerGcsFileSystem() {
#ifdef POLLUX_ENABLE_GCS
  registerFileSystem(isGcsFile, gcsFileSystemGenerator());
  dwio::common::FileSink::registerFactory(
      std::function(gcsWriteFileSinkGenerator));
#endif
}

} // namespace kumo::pollux::filesystems
